package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.bean.Cart;
import com.example.demo.bean.User;
import com.example.demo.service.ICartService;
import com.example.demo.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author 京平笙
 * @since 2021-12-06
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    @Resource
    private ICartService cartService;
    //登录(plus登录 自定义方法)
    @ResponseBody
    @RequestMapping("/LoginServlet.do")
    private ModelAndView loginUser(HttpSession session, User user, HttpServletRequest httpServletRequest, ModelAndView modelAndView ) throws Exception{
        if (userService.exitThisUser(user,session)){
//            查有此人，登录密码正确返回首页
            modelAndView.setViewName("index");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }
//注册
    @ResponseBody
    @RequestMapping("/RegisterServer.do")
    private ModelAndView RegisterServer(HttpSession session, User user, ModelAndView modelAndView ) throws Exception {
//        注册成功回到首页
        if (userService.insertThisUser_Success(user,session)){
            modelAndView.setViewName("redirect:/index");
        }else {
            modelAndView.setViewName("redirect:/register");
        }
        return modelAndView;
    }
//注销
    @ResponseBody
    @RequestMapping("/CancelLogin.do")
    private ModelAndView CancelLogin(@ModelAttribute("User") User user, SessionStatus sessionStatus, HttpSession session, ModelAndView modelAndView)  throws Exception{
        session.setAttribute("user",null);
//        session.setAttribute("id",null);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    //注销用户
    @ResponseBody
    @RequestMapping("/deleteUser.do")
    ModelAndView deleteUser(HttpSession session, ModelAndView modelAndView)  throws Exception{
        User tempUser = (User) session.getAttribute("user");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid",tempUser.getUid()).set("uis_delete",1);

        if ( userService.update(tempUser,updateWrapper)){
//            删除购物车表记录
            boolean remove = cartService.remove(new QueryWrapper<Cart>().eq("uid", tempUser.getUid()));
//                然后退出登录
            modelAndView.setViewName("redirect:/user/CancelLogin.do");
        }else {
            modelAndView.setViewName("redirect:/errorShow");
        }
        return modelAndView;
    }
}

